package com.hoshiicloud.lessee.controller.category;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.query.UploadFileResponse;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.common.validate.ValidGroupAdd;
import com.hoshiicloud.common.validate.ValidGroupUpdate;
import com.hoshiicloud.lessee.feign.LesseeFileUploadService;
import com.hoshiicloud.lessee.feign.LesseeUserFeignService;
import com.hoshiicloud.user.entity.company.AttachmentType;
import com.hoshiicloud.user.param.company.AttachmentTypePageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 2019/8/9 11:19 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category/goods-attachment")
@Api(value = "品类属性-证件管理", tags = {"品类属性-证件管理"})
public class LesseeAttachmentTypeController {
    private final LesseeUserFeignService lesseeUserFeignService;
    private final LesseeFileUploadService fileUpload;
    public LesseeAttachmentTypeController(LesseeUserFeignService lesseeUserFeignService, LesseeFileUploadService fileUpload) {
        this.lesseeUserFeignService = lesseeUserFeignService;
        this.fileUpload = fileUpload;
    }

    @ApiOperation("证件列表分页查询")
    @PostMapping("/getPage")
    public Response<Page<AttachmentType>> getPage(@RequestBody AttachmentTypePageParam param, HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        param.setCompanyId(user.getCompanyId());
        Response<Page<AttachmentType>> rs = lesseeUserFeignService.getAttachmentTypePage(param);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return Response.failed("获取证件类型分页列表失败");
        }
        return rs;
    }

    @ApiOperation("证件示例图片上传                                                                                                                   ")
    @PostMapping("/uploadFile")
    public Response<UploadFileResponse> uploadFile(@RequestPart("file") MultipartFile file) {
        String objectKey = file.getOriginalFilename();
        String fileName = file.getOriginalFilename();
        String bucketName = "obs-7d37";
        String folder = "attachmentType";
        Boolean privateOwned = false;
        Response<UploadFileResponse> rs = fileUpload.fileUpload(bucketName, objectKey, fileName, folder, privateOwned, file);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return Response.failed("获取证件类型分页列表失败");
        }
        return rs;
    }

    @ApiOperation("新增证件")
    @PostMapping("/save")
    public Response<AttachmentType> saveAttachmentType(@RequestBody @Validated(ValidGroupAdd.class) AttachmentType entity,HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        entity.setCompanyId(user.getCompanyId());
        entity.setCreatedBy(user.getRealName());
        Response<AttachmentType> rs = lesseeUserFeignService.addAttachmentType(entity);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return Response.failed("新增证件信息失败");
        }
        return rs;
    }

    @ApiOperation("更新证件")
    @PutMapping("/update")
    public Response<Boolean> updateAttachmentType(@RequestBody @Validated(ValidGroupUpdate.class) AttachmentType entity, HttpServletRequest rq){
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        entity.setUpdatedBy(user.getRealName());
        Response<Boolean> rs = lesseeUserFeignService.updateAttachmentType(entity);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return Response.failed("更新证件信息失败");
        }
        return rs;
    }



    @ApiOperation("删除证件")
    @DeleteMapping("/delete/{id}")
    public Response<Boolean> deleteAttachmentType(@PathVariable("id") Long id){
        Response<Boolean> rs = lesseeUserFeignService.deleteupdateAttachmentType(id);
        if(null == rs || rs.getCode() !=200 || null == rs.getData() ){
            return Response.failed("删除证件信息失败");
        }
        return rs;
    }
}
